package io.jpress.addon.airworthiness.controller;

import com.jfinal.aop.Inject;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;

import io.jboot.db.model.Columns;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jboot.web.validate.EmptyValidate;
import io.jboot.web.validate.Form;
import io.jpress.addon.airworthiness.model.JpressAddonAirworthiness;
import io.jpress.addon.airworthiness.service.JpressAddonAirworthinessService;
import io.jpress.core.menu.annotation.AdminMenu;
import io.jpress.web.base.AdminControllerBase;


@RequestMapping(value = "/admin/airworthiness/jpress_addon_airworthiness", viewPath = "/")
public class _JpressAddonAirworthinessController extends AdminControllerBase {

    @Inject
    private JpressAddonAirworthinessService service;

    @AdminMenu(text = "管理", groupId = "airworthiness")
    public void index() {

        Columns eq = Columns.create()
                .eq("cad_no", getPara("cad_no"))
                .eq("cad_amendment_no",getPara("cad_amendment_no"))
                .eq("title",getPara("title")).or().eq("title_en",getPara("title"))
                .eq("lssued_by",getPara("lssued_by"));

        Page<JpressAddonAirworthiness> entries=service.paginateByColumns(getPagePara(), 10, eq,"effective_date desc");
        setAttr("page", entries);
        render("views/jpress_addon_airworthiness_list.html");
    }


    public void edit() {
        int entryId = getParaToInt(0, 0);
        JpressAddonAirworthiness entry = entryId > 0 ? service.findById(entryId) : null;
        setAttr("jpressAddonAirworthiness", entry);
        render("views/jpress_addon_airworthiness_edit.html");
    }

    public void doSave() {
        JpressAddonAirworthiness entry = getModel(JpressAddonAirworthiness.class,"jpressAddonAirworthiness");
        service.saveOrUpdate(entry);
        renderJson(Ret.ok().set("id", entry.getId()));
    }

    public void doDel() {
        Long id = getIdPara();
        render(service.deleteById(id) ? Ret.ok() : Ret.fail());
    }

    @EmptyValidate(@Form(name = "ids"))
    public void doDelByIds() {
        service.batchDeleteByIds(getParaSet("ids").toArray());
        renderOkJson();
    }
}
